package com.ross.controller;

import com.ross.pojo.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author RossZhang
 */
@Controller
//@RequestMapping(value = "/user")
public class UserController {
    @RequestMapping(value = "/login.do")
    public String login(User user){
        //1.获取主体
        Subject subject = SecurityUtils.getSubject();
        //2、创建令牌
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),user.getPassword() );
        //3、登录
        try {
            subject.login(token);
            //4、登录成功则保存session
            Session session = subject.getSession();
            //保存当前的用户
            session.setAttribute("currentUser",user);
            //返回逻辑视图名称
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            //model.addAttribute("errorInfo","用户名和密码错误");
            return "login";
        }
    }

    @RequestMapping(value = "/index.do")
    public String index(){
        return "index";
    }
}
